package top.cyuw.subjectsystem.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import top.cyuw.subjectsystem.dto.PagingParamDto;
import top.cyuw.subjectsystem.dto.subject.SubjectCreateRequest;
import top.cyuw.subjectsystem.dto.subject.SubjectEditRequest;
import top.cyuw.subjectsystem.service.SubjectService;
import top.cyuw.subjectsystem.vo.PagingVo;
import top.cyuw.subjectsystem.vo.ResponseVo;
import top.cyuw.subjectsystem.vo.subject.SubjectVo;

import java.util.Map;

@RestController
@RequestMapping("/api/subject")
public class SubjectController {

    @Autowired
    SubjectService subjectService;

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    ResponseVo<SubjectVo> getById(@PathVariable Integer id) {
        return ResponseVo.success(subjectService.getById(id));
    }

    @PreAuthorize("hasAuthority('ADMIN')")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    ResponseVo<Boolean> create(@RequestBody SubjectCreateRequest request) {
        return ResponseVo.success(subjectService.create(request));
    }

    @PreAuthorize("hasAuthority('ADMIN')")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    ResponseVo<Boolean> delete(@RequestParam Integer id) {
        return ResponseVo.success(subjectService.delete(id));
    }

    @PreAuthorize("hasAuthority('ADMIN')")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    ResponseVo<Boolean> edit(@RequestBody SubjectEditRequest request) {
        return ResponseVo.success(subjectService.edit(request));
    }

    @RequestMapping(value = "/search", method = RequestMethod.POST)
    ResponseVo<PagingVo<SubjectVo>> search(@RequestBody PagingParamDto<String> request) {
        return ResponseVo.success(subjectService.search(request));
    }

    @RequestMapping(value = "/echart", method = RequestMethod.GET)
    ResponseVo<Map<String, Object>> echart() {
        return ResponseVo.success(subjectService.echart());
    }

}
